package demo.practice.binary_search;

class P29 {

    public static void main(String[] args) {
        P29 p29 = new P29();
        p29.divide(1 << 31, 3);
//        System.out.println((1 << 31) );
    }

    public int divide(int dividend, int divisor) {
        if (dividend==Integer.MIN_VALUE && divisor==-1)
            return Integer.MAX_VALUE;
        boolean isNegative = (dividend > 0 && divisor < 0) || (dividend < 0 && divisor > 0);
        long dividendL = dividend;
        long divisorL =divisor;
        dividendL=Math.abs(dividendL);  //这里不能直接写成  long dividendL=Math.abs(dividend)
        divisorL=Math.abs(divisorL);

        int sum = 0;

        //10  -3*2^1 -3  商为 2^1+1 为3
        while (divisorL <= dividendL) {
            long tmp = divisorL;
            long times = 1;
            while (tmp<<1 <= dividendL) {
                tmp = tmp << 1;
                times = times << 1;
            }
            sum += times;
            dividendL -= tmp;

        }
        return isNegative ? -sum : sum;
    }
}